package com.hpin.mlxteacher.my;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.AddressBookAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.AddressBookResult;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

//import com.umeng.analytics.MobclickAgent;


/**
 * 我的模块的 通讯录
 * Created by Aaron on 15/7/31.
 */
public class AddressBookActivity extends BaseActivity {

    private List<AddressBookResult.Address> msgBeanList = new ArrayList<AddressBookResult.Address>();
    private AddressBookAdapter addItemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_address_book);

        initWidget();

        getData();

    }

    private void initWidget() {

        ImageView tv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.address_book));

        tv_left.setOnClickListener(this);

        ListView lv_address_book = (ListView) findViewById(R.id.lv_address_book);
        addItemAdapter = new AddressBookAdapter(this, msgBeanList);
        lv_address_book.setAdapter(addItemAdapter);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
        }
    }

    public void getData() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("data", sp.getString(Constant.data, ""));

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.MYCOLLEAGUE, j.toJSONString(),
                getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {
                        if (CommonUtils.isNull(str)) {
                            showToast(Constant.NODATA);
                            return;
                        }
                        try {
                            AddressBookResult addressBookResult = JSONObject.parseObject(str, AddressBookResult.class);
                            if (addressBookResult.code == Constant.SUCCESS) {

                                List<AddressBookResult.Address> tempList = addressBookResult.data;

                                if (tempList.size() == 0) showToast(Constant.NOMOREDATA);

                                addItemAdapter.setTempList(tempList);
                                addItemAdapter.notifyDataSetChanged();

                            } else {

                                showToast(Constant.showErrorMsg(addressBookResult.code, AddressBookActivity.this));

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }
                    }
                }, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());

    }

}
